package com.xzz.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: hhz
 * @create: 2022-03-10 09:22
 * 589. N 叉树的前序遍历
 **/
public class Preorder {
    class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }
    public List<Integer> preorder(Node root) {
        List<Integer> res = new ArrayList<>();
        traverse(res,root);
        return res;
    }
    public static void traverse(List<Integer> res,Node node){
        if(node!=null){
            res.add(node.val);
            for (Node child : node.children) {
                traverse(res,child);
            }

        }
    }
}
